/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package compilador.analisadorLexico.automatos;

import compilador.analisadorLexico.CodeReader;
import compilador.analisadorLexico.Token;
import compilador.analisadorLexico.TokenClass;
import compilador.analisadorLexico.TokenErro;

/**
 *
 * @author João Matos
 */
public class AutomatoDigito implements IAutomato{

    private CodeReader reader;

    public AutomatoDigito(CodeReader reader) {
        this.reader = reader;
    }

    public Token extraiToken() {
        
        reader.clear();
        char caracter = reader.next();
        char nextCaracter = reader.next();
        
        final int linha = reader.getCurrentLine();
        Token token = null;
        if (Character.isDigit(caracter)){
            if(Character.isLetter(nextCaracter) || nextCaracter == '_'){

                do{
                    caracter = reader.next();
                }while(Character.isLetter(caracter) || caracter == '_');
                    
                token = new TokenErro(reader.closeToken(), linha, "Dígito mal-formado");
                
            }else if (!Character.isDigit(nextCaracter)){
                token = new Token(TokenClass.NUMERO, reader.closeToken(),linha);
            }
        }

        
        return token;

    }
}
    

